﻿using ExChange.Model;
using System.Data.Entity;
using System.Web.Mvc;
using System.Linq;
using System;
using System.Collections.Generic;

namespace ExChange.Web.Controllers
{
    public class Member_WareController : BaseController
    {
        public JFDHEntities entity = new JFDHEntities();
        public ActionResult Index()
        {
            return View();
        }

  
        public ActionResult GetList(int page, int rows)
        {
            //var list = entity.Member_Ware.Include("Member").ToList(); 
            var list = from o in entity.Member_Ware.Include("Member") select o;
            var totalcount = list.Count();
            var AllWareList = from o in entity.Ware select o;
            var data = list.OrderByDescending(f => f.ID).Skip((page - 1) * rows).Take(rows).ToList().Select(f => new
            {
               f.ID,
               f.Memberlevel,
               f.WareIds,
               WareText = AllWareList.ToList().Where(c => f.WareIds.Contains(c.ID.ToString())).Select(d => d.Name)
            });

            return Json(new { rows = data, total = totalcount }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Save(Member_Ware model)
        {
            string shangpinids = Request["shangpinids"];
            model.WareIds = shangpinids;
            if (model.ID == 0)
            { 
                entity.Member_Ware.Add(model);
            }
            else
            {
                entity.Entry<Member_Ware>(model).State = EntityState.Modified;
            }
            int row = entity.SaveChanges();
            return row > 0 ? Json(new JsonData().Result(true), JsonRequestBehavior.AllowGet) : Json(new JsonData().Result(false));
        }

        public ActionResult Del(int id)
        {
            var model = entity.Member_Ware.Include("Member").FirstOrDefault(f => f.ID == id);
            entity.Member_Ware.Remove(model);
            int row = entity.SaveChanges();
            return row > 0 ? Json(new JsonData().Result(true), JsonRequestBehavior.AllowGet) : Json(new JsonData().Result(false));
        }


        /// <summary>
        /// 得到员工树数据
        /// </summary>
        /// <param name="id">会议id</param>
        /// <param name="status">状态 false为未选中 true为选中</param>
        /// <returns></returns>
        public ActionResult GetTreeWare(bool status, int id)
        {
            List<Ware> list = new List<Ware>();
            if (id == 0)
            {
                if (!status)
                {
                    list = entity.Ware.ToList();
                }
            }
            else
            {
                var ids = entity.Member_Ware.FirstOrDefault(f => f.ID == id).WareIds;
                 list = entity.Ware.ToList();
                if (status)
                {
                    list = list.Where(f => ids.Contains(f.ID.ToString())).ToList();
                }
                else
                {
                    list = list.Where(f => !ids.Contains(f.ID.ToString())).ToList();
                }
            }
            var model = list.Select(o => new
            {
                id = o.ID,
                text = o.Name+"("+o.Pirce+")", 
            });
            return Json(model);
        }


    }
}
